package com.example.ecmftlutil.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class ReadFileCommon {
    public static String CHARSE_GBK = "GBK";
    public static String CHARSE_UTF_8 = "UTF-8";

    public static List<String> readFile(String filePath) {
        return readFile(filePath,CHARSE_GBK);
    }

    public static String readFileStr(String filePath,String charse) {
        List<String> datas = readFile(filePath,charse);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < datas.size(); i++){
            if(i != 0){
                sb.append("\r\n");
            }
            sb.append(datas.get(i));
        }
        return sb.toString();
    }

    public static List<String> readFile(String filePath,String charse) {
        List<String> fileContentList = new ArrayList();
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), charse);//GBK UTF-8
            fileContentList = readInputStreamReader(isr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return fileContentList;
    }

    /*private static Properties getProperties(){
        Properties prop = new Properties();
        try {
            ClassLoader classLoader = CodeGenerator.class.getClassLoader();
            InputStream in =  classLoader.getResourceAsStream("application.properties");
            prop.load(in); /// 加载属性列表
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            prop = null;
        }
        return prop;
    }*/

    public static List<String> readFile(File file) {
        List<String> fileContentList = new ArrayList();
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
            fileContentList = readInputStreamReader(isr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return fileContentList;
    }

    public static List<String> readInputStreamReader(InputStreamReader isr) {
        List<String> fileContentList = new ArrayList();
        try {
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                fileContentList.add(line.trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return fileContentList;
    }

    /*public static File multipartFileToFile(MultipartFile file) {
        File f = null;
        try {
            f = File.createTempFile("tmp", null);
            file.transferTo(f);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }*/

    public static Properties getProperties(String filePath,String charse) {
        Properties p = new Properties();
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), charse);//GBK UTF-8
            p.load(isr);
            isr.close();
        } catch (Exception e) {
            e.printStackTrace();
            p = null;
        }
        return p;
    }
    public static void main(String[] args) {
//        System.out.println(getProperties("C:\\JOB\\csv\\application.properties",ReadFileCommon.CHARSE_UTF_8));
        String path = "C:\\JOB\\csv\\20191219\\10\\QC\\";
        List<String> S24 = readFile(path+"phone.csv");
        List<String> S25 = readFile(path+"all.txt");
        Map<String,String> infos = new HashMap<>();
/*        S24.forEach((obj) -> {
            infos.put(obj,obj);
        } );
        System.out.println("==================开始======================");
        S25.forEach((obj) -> {
            String[] s = obj.split(",");
            if(infos.get(s[1])==null){
                System.out.println(obj);
//                logger.info(obj);
            }
        } );*/
        System.out.println("===================end=======================");
        /*String path = "C:\\JOB\\csv\\20191211\\QC\\";
        List<String> S24 = readFile(path+"24.txt");
        List<String> S25 = readFile(path+"25.txt");
        Map<String,String> infos = new HashMap<>();
        S24.forEach((obj) -> {
            infos.put(obj,obj);
        } );
        S25.forEach((obj) -> {
            String[] s = obj.split(",");
            if(infos.get(s[0])==null){
                System.out.println(obj);
            }
        } );*/
    }
}